package com.lzxuni.modules.disease.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.utils.FileUtil;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.common.utils.date.DateUtil;
import com.lzxuni.modules.basics.entity.BasicTree;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.disease.entity.DiseaseManagement;
import com.lzxuni.modules.disease.service.DiseaseInformationService;
import com.lzxuni.modules.disease.service.DiseaseManagementService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 病情预警信息管理 前端控制器
 * </p>
 *
 * @author gyl
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/Disease/earlyWarningManagement")
public class EarlyWarningManagementController extends BaseController {

    @Autowired
    private DiseaseManagementService diseaseManagementService;
    @Autowired
    private DiseaseInformationService diseaseInformationService;

    private String templatesPath="/Disease/earlyWarningManagement";

    @Autowired
    private FileEntityService fileEntityService;
    private static String typeName = "病情灾害信息图片";
    private static String ywType = "ddi_img";

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/Disease/earlyWarningManagement/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        DiseaseManagement diseaseManagement = JSON.parseObject(queryJson, DiseaseManagement.class);
        diseaseManagement.setIsdis("1");
        diseaseManagement.setIshavedis("1");
        PageData pageData = getPageData(diseaseManagementService.queryPage(pageParameter, diseaseManagement));
        return R.ok().put("data", pageData);
    }


    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, String queryJson, HttpServletRequest request) throws IOException {
        DiseaseManagement diseaseManagement = JSON.parseObject(queryJson, DiseaseManagement.class);
        diseaseManagement.setIsdis("1");
        diseaseManagement.setIshavedis("1");
        //System.out.println(diseaseManagement.toString());
        BufferedOutputStream fos = new BufferedOutputStream(response.getOutputStream());
        String filename = "病情预警信息" + DateUtil.DateToString(new Date(), "yyyyMMddHHmm") + ".xls";
        String agent = request.getHeader("User-Agent");
        filename = FileUtil.encodeDownloadFilename(filename, agent);
        String contentType = request.getSession().getServletContext().getMimeType(filename);
        response.setContentType(contentType);
        response.setHeader("content-disposition", "attchment;filename=" + filename);
        Workbook wb = null;
        try {
            wb = diseaseManagementService.exportExcel(diseaseManagement);
            wb.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fos.close();
        }
    }

}

